package com.jifeng.api.crud.utils;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jifeng.api.crud.constant.FieldConstant;
import com.jifeng.api.crud.entity.TerminalEntity;
import com.jifeng.api.crud.service.TerminalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Objects;

@Component
@Slf4j
public class UpdateStatusRs {
    @Autowired
    TerminalService terminalService;

    /**
     * 更新机柜状态
     * @param terminalEntity 需要更新的机柜
     * @return 更新结果
     */
    public boolean getUpdateTerminalRs(TerminalEntity terminalEntity) {
        boolean updateRs = terminalService.update(terminalEntity,new UpdateWrapper<TerminalEntity>().eq(FieldConstant.T_TERMINAL_IOT,terminalEntity.getIot()));
        if (!updateRs) {
            log.warn("path：{}，errMsg：{}", getRequestUrl(), "更新机柜状态失败");
            return false;
        }
        return true;
    }
    public String getRequestUrl(){
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest().getRequestURI();
    }
}
